package model;

import iedcyt.acute.dataAccess.utils.BeansXML;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import configuration.Log4jConf;

import business.ViewLogger;


import model.thread.ThreadUtil;


/**
 * Modelo para el menu.
 * 
 * @author 53385255R
 *
 */
public class MenuModel {


	

	private ThreadUtil threadUtil;
	
	/**
	 * Constructor
	 */
	public MenuModel() {
		threadUtil = new ThreadUtil();
		
	}
	
	/**
	 * Modifica las rutas de las bases de datos segun las indicadas por parametro
	 * @param dbBeans nombre en el fichero beans y ruta de las bases de datos
	 * @return si el cambio se realiza correctamente
	 */
	public boolean changeDatabases(HashMap<String, String> dbBeans) {
		
		
		//Mete en el fichero beansXml del proyecto dataaccess las nuevas rutas
		for (String key : dbBeans.keySet()) {
			BeansXML.getInstance().setDatabasePath(key, dbBeans.get(key));
		}
		//Graba los cambios
		return BeansXML.getInstance().saveChanges();
	}
	
	
	
	
	/**
	 * Ejecuta el programa especificado abriendo el fichero de log
	 * @param mtailfile
	 */
	public void openLog(String mtailfile) {
		try {
			//Cogemos un fichero en la raiz de la aplicacion para abrir
			Log4jConf l4jCnf = new Log4jConf();
			File archivo=new File(l4jCnf.getLogFilePath()); 
			Runtime.getRuntime().exec(mtailfile + " \"" + archivo.getAbsolutePath() + "\"");
		} catch (IOException e) {
			ViewLogger.getInstance().error("Error al abrir el log");
		}
		
	}

	/**
	 * Obtencion del campo funding
	 */
	public void fundedRetrieve() {
		//TODO: pendiente meter hilos indicando en el parametro el numero de hilo
		threadUtil.executeWithThread(threadUtil.METHOD_RETRIEVE_FUNDING, new String[] {"1"});
		
	}

	public void TCRetrieve() {
		//TODO: pendiente meter hilos indicando en el parametro el numero de hilo
		//threadUtil.executeWithThread(threadUtil.METHOD_RETRIEVE_TC, new String[] {"1"} );
		threadUtil.executeWithThread(threadUtil.METHOD_RETRIEVE_TC, new String[] {"1"} );
	}

	public void WoSCitingDownload(String address, String year, String folder, Integer onlyIndex, Integer fromIndex) {
		threadUtil.executeWithThread(threadUtil.METHOD_DOWNLOAD_WOS_CITING, new String[] { address,year, folder, onlyIndex+"", fromIndex+"" });
		
	}

	public void WoSCitingDownloadByUtid(String folder) {
		threadUtil.executeWithThread(threadUtil.METHOD_DOWNLOAD_WOS_CITING_BY_UTID, new String[] { folder });
		
	}
	
	public void generateUrl() {
		threadUtil.executeWithThread(threadUtil.METHOD_DOWNLOAD_WOS_CITING_BY_URL_FIRST, null);
		
	}
	
	public void WoSCitingDownloadByURL(String folder, String url) {
		threadUtil.executeWithThread(threadUtil.METHOD_DOWNLOAD_WOS_CITING_BY_URL_SECOND, new String[]{folder, url});
	
	}
	public void failWoSCitingDownload(String address, String folder) {
		threadUtil.executeWithThread(threadUtil.METHOD_FAIL_REPAIR_WOS_CITING, new String[] { address, folder });
	}

	public void TxtTratamiento(String bool) {
		threadUtil.executeWithThread(threadUtil.METHOD_PROCESSING_TXT_FILE, new String[]{bool});
	}

	public void RetrieveUtid() {
		threadUtil.executeWithThread(threadUtil.METHOD_ADD_UTID, null);
		
	}

	

	
}
